//
//  WeiboRefreshControl.swift
//  MyWeibo
//
//  Created by tanchao on 15/8/4.
//  Copyright © 2015年 chaoren. All rights reserved.
//

import UIKit
private let kRefreshPullOffset: CGFloat = -60
class WeiboRefreshControl: UIRefreshControl {
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) 	{
        if frame.origin.y > 0{
//            if refreshing{
//                endRefreshing()
//            }
            return
        }
        if refreshing{
            print(frame)
            refreshView.startLoading()
        }
        if frame.origin.y < kRefreshPullOffset && !refreshView.rotateFlag{
           refreshView.rotateFlag = true
        }else if frame.origin.y > kRefreshPullOffset && refreshView.rotateFlag{
            refreshView.rotateFlag = false
        }
    }
    override func endRefreshing() {
        super.endRefreshing()
        print("停止")
        refreshView.stopLoading()
    }
    override init() {
        super.init()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    deinit{
        self.removeObserver(self, forKeyPath: "frame")
    }
    private func setupUI(){
        self.addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
        addSubview(refreshView)
        refreshView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: refreshView.bounds.size)
       
    }
    lazy private  var refreshView: WeiboRefreshView = WeiboRefreshView.refreshView()

}
class WeiboRefreshView : UIView {
    /// 旋转标记
    private var rotateFlag = false {
        didSet {
            rotationtipIcon()
        }
    }

    @IBOutlet weak var loadingIcon: UIImageView!
    @IBOutlet weak var tipView: UIView!
    @IBOutlet weak var tipLabel:UILabel!
    @IBOutlet weak var tipIcon:UIImageView!
    
    class func refreshView()->WeiboRefreshView {
        return NSBundle.mainBundle().loadNibNamed("WeiboRefresh", owner: nil, options: nil).last as! WeiboRefreshView
    }
    
    /// 提示图标的旋转
    private func rotationtipIcon() {
        // 在块代码动画中，旋转会就近原则，默认顺时针寻找方向
        let angle = rotateFlag ? CGFloat(M_PI - 0.01) : CGFloat(M_PI + 0.01)
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.tipIcon.transform = CGAffineTransformRotate(self.tipIcon.transform, angle)
            self.tipLabel.text = self.rotateFlag ? "释放更新": "下拉刷新"
        }
    }
    private func startLoading(){
        if loadingIcon.layer.animationForKey("loadingAnim") != nil {
            return
        }
        tipView.hidden = true
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 1.0
        loadingIcon.layer.addAnimation(anim, forKey: "loadingAnim")
    }
    private func stopLoading(){
        tipView.hidden = false
        loadingIcon.layer.removeAllAnimations()
        
    
    
    
    }

}